package com.iphone.extra.view;

import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

public abstract class IWidgetFrameLayout extends FrameLayout implements View.OnClickListener, OnWidgetEventListener {

    private boolean isIgnoreClick = false;
    private final Point mDownPoint = new Point();

    public IWidgetFrameLayout(Context context) {
        super(context);
    }

    public IWidgetFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public IWidgetFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setOnClickListener(this);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int x = (int) ev.getRawX();
        int y = (int) ev.getRawY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownPoint.set(x, y);
                isIgnoreClick = false;
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                int deltaX = mDownPoint.x - x;
                if (Math.abs(deltaX) > 10) {
                    isIgnoreClick = true;
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public final void onClick(View v) {
        if (!isIgnoreClick) {
            onCellClicked(getDescribe());
        }
    }

    @Override
    public String getDescribe() {
        return "";
    }
}
